package service

import (
	"bloggo/structs"
	"context"
	"errors"
	"fmt"
	"gitee.com/chunanyong/logger"
	"gitee.com/chunanyong/zorm"
)

//SaveTbOperationLogStruct 保存
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func SaveTbOperationLogStruct(ctx context.Context, tboperationlogstruct *structs.TbOperationLogStruct) error {

	// tboperationlogstruct对象指针不能为空
	if tboperationlogstruct == nil {
		return errors.New("tboperationlogstruct对象指针不能为空")
	}
	//匿名函数return的error如果不为nil,事务就会回滚
	_, errSaveTbOperationLogStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始

		_, errSaveTbOperationLogStruct := zorm.Insert(ctx, tboperationlogstruct)

		if errSaveTbOperationLogStruct != nil {
			return nil, errSaveTbOperationLogStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errSaveTbOperationLogStruct != nil {
		errSaveTbOperationLogStruct := fmt.Errorf("service.SaveTbOperationLogStruct错误:%w", errSaveTbOperationLogStruct)
		logger.Error(errSaveTbOperationLogStruct)
		return errSaveTbOperationLogStruct
	}

	return nil
}

//UpdateTbOperationLogStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateTbOperationLogStruct(ctx context.Context, tboperationlogstruct *structs.TbOperationLogStruct) error {

	// tboperationlogstruct对象指针或主键Id不能为空
	if tboperationlogstruct == nil || (tboperationlogstruct.Id) < 1 {
		return errors.New("tboperationlogstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbOperationLogStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbOperationLogStruct := zorm.Update(ctx, tboperationlogstruct)

		if errUpdateTbOperationLogStruct != nil {
			return nil, errUpdateTbOperationLogStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbOperationLogStruct != nil {
		errUpdateTbOperationLogStruct := fmt.Errorf("service.UpdateTbOperationLogStruct错误:%w", errUpdateTbOperationLogStruct)
		logger.Error(errUpdateTbOperationLogStruct)
		return errUpdateTbOperationLogStruct
	}

	return nil
}

//UpdateNotZeroTbOperationLogStruct 更新
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func UpdateNotZeroTbOperationLogStruct(ctx context.Context, tboperationlogstruct *structs.TbOperationLogStruct) error {

	// tboperationlogstruct对象指针或主键Id不能为空
	if tboperationlogstruct == nil || (tboperationlogstruct.Id) < 1 {
		return errors.New("tboperationlogstruct对象指针或主键Id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errUpdateTbOperationLogStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		_, errUpdateTbOperationLogStruct := zorm.UpdateNotZeroValue(ctx, tboperationlogstruct)

		if errUpdateTbOperationLogStruct != nil {
			return nil, errUpdateTbOperationLogStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errUpdateTbOperationLogStruct != nil {
		errUpdateTbOperationLogStruct := fmt.Errorf("service.UpdateNotZeroTbOperationLogStruct错误:%w", errUpdateTbOperationLogStruct)
		logger.Error(errUpdateTbOperationLogStruct)
		return errUpdateTbOperationLogStruct
	}

	return nil
}

//DeleteTbOperationLogStructById 根据Id删除
//如果入参ctx中没有dbConnection,使用defaultDao开启事务并最后提交
//如果入参ctx有dbConnection且没有事务,调用dbConnection.begin()开启事务并最后提交
//如果入参ctx有dbConnection且有事务,只使用不提交,有开启方提交事务
//但是如果遇到错误或者异常,虽然不是事务的开启方,也会回滚事务,让事务尽早回滚
func DeleteTbOperationLogStructById(ctx context.Context, id string) error {

	//id不能为空
	if len(id) < 1 {
		return errors.New("id不能为空")
	}

	//匿名函数return的error如果不为nil,事务就会回滚
	_, errDeleteTbOperationLogStruct := zorm.Transaction(ctx, func(ctx context.Context) (interface{}, error) {

		//事务下的业务代码开始
		finder := zorm.NewDeleteFinder(structs.TbOperationLogStructTableName).Append(" WHERE id=?", id)
		_, errDeleteTbOperationLogStruct := zorm.UpdateFinder(ctx, finder)

		if errDeleteTbOperationLogStruct != nil {
			return nil, errDeleteTbOperationLogStruct
		}

		return nil, nil
		//事务下的业务代码结束

	})

	//记录错误
	if errDeleteTbOperationLogStruct != nil {
		errDeleteTbOperationLogStruct := fmt.Errorf("service.DeleteTbOperationLogStruct错误:%w", errDeleteTbOperationLogStruct)
		logger.Error(errDeleteTbOperationLogStruct)
		return errDeleteTbOperationLogStruct
	}

	return nil
}

//FindTbOperationLogStructById 根据Id查询信息
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbOperationLogStructById(ctx context.Context, id string) (*structs.TbOperationLogStruct, error) {
	//id不能为空
	if len(id) < 1 {
		return nil, errors.New("id不能为空")
	}

	//根据Id查询
	finder := zorm.NewSelectFinder(structs.TbOperationLogStructTableName).Append(" WHERE id=?", id)
	tboperationlogstruct := structs.TbOperationLogStruct{}
	has, errFindTbOperationLogStructById := zorm.QueryRow(ctx, finder, &tboperationlogstruct)

	//记录错误
	if errFindTbOperationLogStructById != nil {
		errFindTbOperationLogStructById := fmt.Errorf("service.FindTbOperationLogStructById错误:%w", errFindTbOperationLogStructById)
		logger.Error(errFindTbOperationLogStructById)
		return nil, errFindTbOperationLogStructById
	}

	if !has {
		return nil, nil
	}

	return &tboperationlogstruct, nil

}

//FindTbOperationLogStructList 根据Finder查询列表
//ctx中如果没有dbConnection,则会使用默认的datasource进行无事务查询
func FindTbOperationLogStructList(ctx context.Context, finder *zorm.Finder, page *zorm.Page) ([]structs.TbOperationLogStruct, error) {

	//finder不能为空
	if finder == nil {
		return nil, errors.New("finder不能为空")
	}

	tboperationlogstructList := make([]structs.TbOperationLogStruct, 0)
	errFindTbOperationLogStructList := zorm.Query(ctx, finder, &tboperationlogstructList, page)

	//记录错误
	if errFindTbOperationLogStructList != nil {
		errFindTbOperationLogStructList := fmt.Errorf("service.FindTbOperationLogStructList错误:%w", errFindTbOperationLogStructList)
		logger.Error(errFindTbOperationLogStructList)
		return nil, errFindTbOperationLogStructList
	}

	return tboperationlogstructList, nil
}
